package com.bidcms.util;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class templateUtil {
	public String goods_list(JsonElement jsonarr,JsonObject json){
		List<String> size_type = new ArrayList<String>();
		size_type.add("card");
		size_type.add("waterfall");
		size_type.add("normal");
		List<String> sizes = new ArrayList<String>();
		sizes.add("big-pic");
		sizes.add("small-pic");
		sizes.add("");
		String listtype = json.get("size").getAsInt()==3?"list":"pic";
		StringBuilder sb = new StringBuilder();
		sb.append("<ul class=\"js-goods-list sc-goods-list ")
		.append(listtype).append(" ")
		.append("clearfix size-")
		.append(json.get("size").getAsInt())
		.append(" \" data-size=\"")
		.append(json.get("size").getAsInt())
		.append("\" data-showtype=\"")
		.append(size_type.get(json.get("size_type").getAsInt()))
		.append("\" style=\"visibility: visible;\">");
		
		if (jsonarr.isJsonArray()) {
			JsonArray jsonarray = (JsonArray)jsonarr;
			for(int i =0;i<jsonarray.size();i++){
				JsonObject item =(JsonObject) jsonarray.get(i);
				String size = json.get("size").getAsInt()==2?(i%3==0?"big-pic":"small-pic"):sizes.get(json.get("size").getAsInt());
				
				sb.append("<li class=\"js-goods-card goods-card ")
				.append(size)
				.append(" ")
				.append(size_type.get(json.get("size_type").getAsInt()))
				.append("\">")
				.append("<a href=\"index?con=show&act=goods&id=")
				.append(item.get("id").getAsInt())
				.append("\" ")
				.append(" class=\"js-goods link clearfix\" target=\"_blank\" data-goods-id=\"")
				.append(item.get("id").getAsInt())
				.append("\" ")
				.append("title=\"")
				.append(item.get("title").getAsString())
				.append("\"><div class=\"photo-block\" data-width=\"128\" data-height=\"150\" style=\"background-color: rgb(255, 255, 255);\"><img class=\"goods-photo js-goods-lazy\" data-src=\"")
				.append(item.get("image_url").getAsString())
				.append("\" src=\"")
				.append(item.get("image_url").getAsString())
				.append("\" style=\"display: inline;\"></div><div class=\"info clearfix info-title info-price btn")
				.append(json.get("buy_btn_type").getAsString())
				.append(" ")
				//($rule['show_title']+$rule['price']==0?'hide':'')
				.append(json.get("show_title").getAsInt()+json.get("price").getAsInt()==0?"hide":"")
				.append("\">");
				if(json.get("show_title").getAsInt()==1){
					sb.append("<p class=\"goods-title c-black\">")
					.append(item.get("title").getAsString())
					.append("</p>");
				}
				if(json.get("show_tag_title").getAsInt()==1){
					sb.append("<p class=\"goods-sub-title c-black hide\"></p>");
				}
				if(json.get("price").getAsInt()==1){
					sb.append("<p class=\"goods-price\"><em>￥")
					.append(item.get("price").getAsInt())
					.append("'</em></p>");
				}
				sb.append("<p class=\"goods-price-taobao hide\"></p></div>");
				if(json.get("buy_btn").getAsInt()>0&&json.get("size_type").getAsInt()!=2)
				{
					sb.append("<div class=\"goods-buy btn")
					.append(json.getAsJsonPrimitive("buy_btn_type").getAsInt())
					.append(" info-title\"></div><div class=\"js-goods-buy buy-response\" data-alias=\"")
					.append(item.get("alias").getAsString())
					.append("\" data-postage=\"0\" data-buyway=\"1\" data-id=\"")
					.append(item.get("id").getAsInt())
					.append("\" data-title=\"")
					.append(item.get("id").getAsString())
					.append("\" data-price=\"")
					.append("\" data-isvirtual=\"0\"></div>");
					
				}
				sb.append("	</a></li>");
			}
		}else {

			JsonObject item =(JsonObject) jsonarr;
			String size = json.get("size").getAsString();
			
			sb.append("<li class=\"js-goods-card goods-card ")
			.append(size)
			.append(" ")
			.append(size_type.get(json.get("size_type").getAsInt()))
			.append("\">")
			.append("<a href=\"index?con=show&act=goods&id=")
			.append(item.get("id").getAsInt())
			.append("\" ")
			.append(" class=\"js-goods link clearfix\" target=\"_blank\" data-goods-id=\"")
			.append(item.get("id").getAsInt())
			.append("\" ")
			.append("title=\"")
			.append(item.get("title").getAsString())
			.append("\"><div class=\"photo-block\" data-width=\"128\" data-height=\"150\" style=\"background-color: rgb(255, 255, 255);\"><img class=\"goods-photo js-goods-lazy\" data-src=\"")
			.append(item.get("image_url"))
			.append("\" src=\"")
			.append(item.get("image_url"))
			.append("\" style=\"display: inline;\"></div><div class=\"info clearfix info-title info-price btn")
			.append(json.get("buy_btn_type"))
			.append(" ")
			//($rule['show_title']+$rule['price']==0?'hide':'')
			.append(null == json.get("show_title")?0:json.get("show_title").getAsInt()+json.get("price").getAsInt()==0?"hide":"")
			.append("\">");
			if(null != json.get("show_title") && json.get("show_title").getAsInt()==1){
				sb.append("<p class=\"goods-title c-black\">")
				.append(item.get("title").getAsString())
				.append("</p>");
			}
			if(null != json.get("show_tag_title") && json.get("show_tag_title").getAsInt()==1){
				sb.append("<p class=\"goods-sub-title c-black hide\"></p>");
			}
			if(null != json.get("price")&& json.get("price").getAsInt()==1){
				sb.append("<p class=\"goods-price\"><em>￥")
				.append(item.get("price"))
				.append("'</em></p>");
			}
			sb.append("<p class=\"goods-price-taobao hide\"></p></div>");
			if(json.get("buy_btn").getAsInt()>0&&json.get("size_type").getAsInt()!=2)
			{
				sb.append("<div class=\"goods-buy btn")
				.append(json.getAsJsonPrimitive("buy_btn_type").getAsInt())
				.append(" info-title\"></div><div class=\"js-goods-buy buy-response\" data-alias=\"")
				.append(item.get("alias").getAsString())
				.append("\" data-postage=\"0\" data-buyway=\"1\" data-id=\"")
				.append(item.get("id").getAsInt())
				.append("\" data-title=\"")
				.append(item.get("id").getAsString())
				.append("\" data-price=\"")
				.append("\" data-isvirtual=\"0\"></div>");
				
			}
			sb.append("	</a></li>");
		
		}
		
		sb.append("</ul>");
		return sb.toString();
	}
}
